"""
Class implements storage engine as module for BlogoSerV
"""

import os

# err_codes
R_OK    = 0
R_ERROR = 1

class mod_storage( ):
  """
  """

  def __init__( self ):
    pass

  def do_put( self, authContext, args ):
    return ( R_OK, [] )

  def do_get( self, authContext, args ):
    if type( args ) is list and len( args ) == 1:

      try:
        fd = open( '%s/%s' % ( authContext[2], args[0] ), 'r' )

      except:
        return ( R_ERROR, ['No such file'] )

      else:
        return ( R_OK, [fd] )

  def do_ls( self, authContext, args ):
    res = []

    # authContext: ( id, name, path )
    if type( args ) is list and len( args ) > 0:
      fd = os.popen( 'ls %s/%s' % ( authContext[2], args[0] ), 'r' )
      res = fd.read().split('\n')[:-1]
      fd.close()

    else:
      fd = os.popen( 'ls %s' % ( authContext[2] ), 'r' )
      res = fd.read().split('\n')[:-1]
      fd.close()

    return ( R_OK, res )
